<template>
  <div class="login-box">
    <div class="form-box">
      <el-card class="box-card">
        <div slot="header" class="clearfix header">
          <span>黑马面经运营后台</span>
        </div>
        <el-form ref="form" :rules="rules" :model="form">
          <el-form-item label="用户名" prop="username">
            <el-input
              placeholder="请输入用户名"
              v-model="form.username"
              @keyup.enter.native="fn"
            ></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input
              placeholder="请输入密码"
              v-model="form.password"
              type="password"
              @keyup.enter.native="login"
              ref="pwd"
            ></el-input>
          </el-form-item>
          <el-form-item class="btns">
            <el-button type="primary" @click="login" :loading="flag"
              >登录</el-button
            >
            <el-button @click="reset">重置</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>
import { login } from '@/api/user'
export default {
  name: 'login-page',
  components: {},
  props: {},
  data() {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 5, max: 11, message: '用户名长度5-11位', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 5, max: 11, message: '密码长度5-11位', trigger: 'blur' }
        ]
      },
      flag: false
    }
  },
  computed: {},
  watch: {},
  created() {},
  methods: {
    async login() {
      //   console.log(123)
      try {
        await this.$refs.form.validate()
        // 到这里说明校验通过准备发请求
        // 这里发送ajax
        this.flag = true
        const res = await login(this.form)
        console.log(res)
        this.flag = false
        this.$store.commit('user/setToken', res.data.token)
        this.$message.success('登录成功')
        this.$router.push('/')
      } catch (error) {
        console.dir(error)
        // this.$message.error('校验失败')
        if (error.response && error.response.status === 400) {
          this.$message.error('用户名或者密码不存在')
        } else {
          this.$message.error('出错了')
        }
        this.flag = false
      }
    },
    reset() {
      this.$refs.form.resetFields()
    },
    fn() {
      console.log(123)
      this.$refs.pwd.focus()
    }
  }
}
</script>

<style scoped lang="scss">
.login-box {
  width: 100vw;
  height: 100vh;
  background: url('@/assets/login-bg.svg') no-repeat center center;
  display: flex;
  justify-content: center;
  align-items: center;
  .form-box {
    width: 420px;
    background-color: pink;
    .header {
      text-align: center;
      color: #fff;
    }
  }
  ::v-deep .el-card__header {
    background-color: #727cf5;
  }
}
.btns {
  text-align: center;
  .el-button:first-child {
    background-color: #727cf5;
  }
}
</style>
